﻿using bookmeetingroom.Data;
using bookmeetingroom.Data.Entities;
using bookmeetingroom.Helper;
using bookmeetingroom.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace bookfacility.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Produces("application/json")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class FacilityController : ControllerBase
    {
        private readonly ApplicationDbContext _dbContext;
        private readonly ILogger<FacilityController> _logger;

        public FacilityController(ApplicationDbContext dbContext, ILogger<FacilityController> logger)
        {
            _dbContext = dbContext;
            _logger = logger;

        }

        [HttpGet]
        public ActionResult List()
        {
            Guid guid = this.User.GetCompanyID();

            var facilitys = _dbContext.Facilities.Where(n =>  n.CompanyId == guid).OrderByDescending(n => n.UpdatedTime).ToArray();

            return Ok(new { message = "", data = facilitys });

        }
        [HttpPost]
        public async Task<ActionResult> Create(FacilityModel facilityModel)
        {
            if (!ModelState.IsValid) return BadRequest(ModelState.GetErrorMessages());

            Facility facility = new Facility
            {
                Name = facilityModel.Name,
          
                CompanyId = this.User.GetCompanyID(),
            };

            facility.MarkAdded();

            _dbContext.Add(facility);

            await _dbContext.SaveChangesAsync();

            var guid = this.User.GetCompanyID();

            var facilitys = await _dbContext.Facilities.Where(n => n.CompanyId == guid).OrderByDescending(n => n.UpdatedTime).ToArrayAsync();

            return Ok(new { message = "建立成功", data = facilitys });

        }
        [HttpGet("{id}")]
        public async Task<ActionResult> Detail(int id)
        {
            Guid guid = this.User.GetCompanyID();

            var facility = await _dbContext.Facilities.Where(n => n.CompanyId == guid && n.Id == id)
                .SingleOrDefaultAsync();

            return Ok(new { message = "", data = facility });
        }
        [HttpPost("Remove/{id}")]
        public async Task<ActionResult> Remove(int? id)
        {
            if (!id.HasValue) return BadRequest("不能找到设施");

            var facility = await _dbContext.Facilities.FindAsync(id.Value);

            if (facility != null)
            {
                _dbContext.Facilities.Remove(facility);

                await _dbContext.SaveChangesAsync();
            }

            var guid = this.User.GetCompanyID();

            var facilities = await _dbContext.Facilities.Where(n => n.CompanyId == guid).OrderByDescending(n => n.UpdatedTime).ToArrayAsync();

            return Ok(new { message = "删除成功", data = facilities });


        }

    }
}
